﻿using GalaSoft.MvvmLight;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using Jn698._45Tool.Service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using TouchMvvm;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels.Controls.DataControls
{
    public class StepPriceViewModel:ViewModelBase
    {
        private GWCmdMakeUpService GWCmdMakeUpService = new GWCmdMakeUpService();
        private GWCmdMakeUpPlanService GWCmdMakeUpPlanService = new GWCmdMakeUpPlanService();
        public static GWCmdMakeUp GWCmdMakeUp;
        public static GWCmdMakeUpPlan GWCmdMakeUpPlan;
        private BindingList<SingleData> stepDataList;

		public BindingList<SingleData> StepDataList
        {
			get { return stepDataList; }
			set { stepDataList = value; RaisePropertyChanged();  }
		}

        private ObservableCollection<SingleData> stepPriceList;

        public ObservableCollection<SingleData> StepPriceList
        {
            get { return stepPriceList; }
            set { stepPriceList = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<SettlementData> stepSettlementList;

        public ObservableCollection<SettlementData> StepSettlementList
        {
            get { return stepSettlementList; }
            set { stepSettlementList = value; RaisePropertyChanged(); }
        }

        public IEventAction[] DataGridActionsCommand { get; set; }
        public StepPriceViewModel()
        {
            StepSettlementList = new ObservableCollection<SettlementData>();
            this.DataGridActionsCommand = new IEventAction[]
            {
                new EventAction<object,DataGridCellEditEndingEventArgs>("CellEditEnding",DataGridCellEditEndingChanged),
                new EventAction<object,KeyEventArgs>("PreviewKeyDown",KeyDown),

            };

        }

        private void DataGridCellEditEndingChanged(object arg1, DataGridCellEditEndingEventArgs args)
        {
            if (args.EditAction == DataGridEditAction.Commit)
            {
                if (arg1 is DataGrid dataGrid)
                {
                    var el = args.EditingElement as TextBox;
                    switch (dataGrid.Name)
                    {
                        case "dataGridStepData":
                            var column = args.Column as DataGridBoundColumn;
                            SingleData c = StepDataList[dataGrid.SelectedIndex];
                            if (column != null)
                            {
                                StepDataList[dataGrid.SelectedIndex].Name = el.Text;
                                SaveData();
                            }
                            break;
                        case "dataGridStepPrice":
                           
                            column = args.Column as DataGridBoundColumn;
                            c = StepPriceList[dataGrid.SelectedIndex];
                            if (column != null)
                            {
                                StepPriceList[dataGrid.SelectedIndex].Name = el.Text;
                                SaveData();
                            }
                            break;


                        case "dataGridStepSettlement":
                            
                            column = args.Column as DataGridBoundColumn;
                            SettlementData t = StepSettlementList[dataGrid.SelectedIndex];
                            if (column != null)
                            {
                                switch (column.DisplayIndex)
                                {
                                    case 0:
                                        t.Month = el.Text;
                                        break;
                                    case 1:
                                        t.Day = el.Text;
                                        break;
                                    case 2:
                                        t.Hour = el.Text;
                                        break;
                                    default:
                                        break;
                                }
                                SaveData();
                            }
                            break;

                        default:
                            break;
                    }

                }
            }

        }

        private void KeyDown(object arg1, KeyEventArgs args)
        {
            if (args.Key == Key.Delete)
            {
                // 在这里编写处理删除键的代码
                if (arg1 is DataGrid dataGrid)
                {
                    var index = dataGrid.SelectedIndex;
                    switch (dataGrid.Name)
                    {
                        case "dataGridStepData":
                            StepDataList.RemoveAt(index);
                            SaveData();
                            break;
                        case "dataGridStepPrice":
                            StepPriceList.RemoveAt(index);
                            SaveData();
                            break;
                        case "dataGridStepSettlement":
                            StepSettlementList.RemoveAt(index);
                            SaveData();
                            break;

                        default:
                            break;
                    }
                }
            }
        }
        public void UpdateData()
        {
            string[] datas;
            if (MainViewModel.TopIndex==0)
            {
                if (GWCmdMakeUp == null)
                {
                    return;
                }
                if (GWCmdMakeUp.SubItemName == null)
                {
                    return;
                }
                datas = GWCmdMakeUp.DataValue.Split(';');
            }
            else
            {
                if (GWCmdMakeUpPlan == null)
                {
                    return;
                }
                if (GWCmdMakeUpPlan.SubItemName == null)
                {
                    return;
                }
                datas = GWCmdMakeUpPlan.DataValue.Split(';');
            }

            var stepDatas = datas[0].Split(',');
            StepDataList = new BindingList<SingleData>(stepDatas.Select(n => new SingleData { Name = n }).ToList());
            var stepPrices = datas[1].Split(',');
            StepPriceList = new ObservableCollection<SingleData>(stepPrices.Select(n => new SingleData { Name = n }));
            var stepSettlements = datas[2].Split(',');
            var num = stepSettlements.Length / 3;
            StepSettlementList.Clear();
            for (int i = 0; i < num; i++)
            {
                SettlementData settlementData = new SettlementData();
                settlementData.Month = stepSettlements[3*i];
                settlementData.Day = stepSettlements[3 * i + 1];
                settlementData.Hour = stepSettlements[3 * i + 2];
                StepSettlementList.Add(settlementData);
            }
        }


        public void SaveData()
        {
            if (StepDataList==null|| StepPriceList==null|| StepSettlementList==null)
            {
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < StepDataList.Count; i++)
            {
                stringBuilder.Append(StepDataList[i].Name);
                if (i+1!= StepDataList.Count)
                {
                    stringBuilder.Append(',');
                } 
            }
            stringBuilder.Append(';');
            for (int i = 0; i < StepPriceList.Count; i++)
            {
                stringBuilder.Append(StepPriceList[i].Name);
                if (i + 1 != StepPriceList.Count)
                {
                    stringBuilder.Append(',');
                }
            }
            stringBuilder.Append(';');
            for (int i = 0; i < StepSettlementList.Count; i++)
            {
                SettlementData settlementData = StepSettlementList[i];

                if (string.IsNullOrWhiteSpace(settlementData.Month)|| string.IsNullOrWhiteSpace(settlementData.Day)||string.IsNullOrWhiteSpace(settlementData.Hour))
                {
                    if (MainViewModel.TopIndex == 0)
            {
                GWCmdMakeUp.DataValue = stringBuilder.ToString();
                GWCmdMakeUpService.Update(GWCmdMakeUp);
            }
            else
            {
                GWCmdMakeUpPlan.DataValue = stringBuilder.ToString();
                GWCmdMakeUpPlanService.Update(GWCmdMakeUpPlan);
            }
                    return;
                }
                stringBuilder.Append(settlementData.Month);
                stringBuilder.Append(',');
                stringBuilder.Append(settlementData.Day);
                stringBuilder.Append(',');
                stringBuilder.Append(settlementData.Hour);
                if (i + 1 != StepSettlementList.Count)
                {
                    stringBuilder.Append(',');
                }
            }
            if (MainViewModel.TopIndex == 0)
            {
                GWCmdMakeUp.DataValue = stringBuilder.ToString();
                GWCmdMakeUpService.Update(GWCmdMakeUp);
            }
            else
            {
                GWCmdMakeUpPlan.DataValue = stringBuilder.ToString();
                GWCmdMakeUpPlanService.Update(GWCmdMakeUpPlan);
            }
        }
    }
}
